﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WangMiEntity.EmployeeService;
using WangMiBusiness.GridViewModel;
using WangMiEntity.Order;

namespace WangMiBusiness.EmployeeService
{
    public class KeJianShuBusiness
    {
        /// <summary>
        /// 按日期排列
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="session"></param>
        /// <returns></returns>
        public List<KeJianShuDateEntity> GetKeJianShuByDate(DateTime startTime, DateTime endTime, string session, bool isContainALLDate)
        {
            List<KeJianShuDateEntity> keJianShuDateEntityList = new List<KeJianShuDateEntity>();
            var gridViewModel = new GridViewModelBusiness().GetBaseGridViewModel(startTime, endTime, session);
            var orderPriceViewModel = GridViewHelper.GetOrderPrice(gridViewModel, string.Empty);

            orderPriceViewModel.ForEach(item =>
            {
                KeJianShuDateEntity keJianShuDateEntity = new KeJianShuDateEntity();
                keJianShuDateEntity.OrderDate = item.OrderDate;

                //总客件数
                keJianShuDateEntity.TotalCustomerCount = GetTotalCustomerCount(item);

                //未付款客件数
                keJianShuDateEntity.UnpaidCustomerCount = item.WaitBuyerPayOrderCount == 0 ? 0 : item.WaitBuyerPayTradeCount / item.WaitBuyerPayOrderCount;

                //静默已付款客件数
                keJianShuDateEntity.SilentPaidCustomerCount = GetPaidCustomerCount(item);

                //团队已付款客件数
                keJianShuDateEntity.TeamPaidCustomerCount = GetPaidCustomerCount(item);

                //退款中客件数
                keJianShuDateEntity.RefundingCustomerCount = item.RefundingTradeCount == 0 ? 0 : item.RefundingOrderCount / item.RefundingTradeCount;

                //已退款客件数
                keJianShuDateEntity.RefundCustomerCount = item.RefundTradeCount == 0 ? 0 : item.RefundOrderCount / item.RefundTradeCount;

                keJianShuDateEntityList.Add(keJianShuDateEntity);
            });

            return keJianShuDateEntityList;
        }

        /// <summary>
        /// 获取总客件数
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        protected int GetTotalCustomerCount(OrderPrice orderPrice)
        {
            int totalTradeCount = 0;
            int totalOrderCount = 0;

            totalTradeCount += orderPrice.SellerConsignedPartTradeCount;
            totalTradeCount += orderPrice.TradeBuyerSignedTradeCount;
            totalTradeCount += orderPrice.TradeNoCreatePayTradeCount;
            totalTradeCount += orderPrice.WaitBuyerConfirmGoodsTradeCount;
            totalTradeCount += orderPrice.WaitBuyerPayTradeCount;
            totalTradeCount += orderPrice.WaitSellerSendGoodsTradeCount;
            totalTradeCount += orderPrice.RefundingTradeCount;
            totalTradeCount += orderPrice.RefundTradeCount;
            totalTradeCount += orderPrice.TradeClosedTradeCount;
            totalTradeCount += orderPrice.TradeFinishedTradeCount;
            totalTradeCount += orderPrice.TradeClosedByTaoBaoTradeCount;

            totalOrderCount += orderPrice.SellerConsignedPartOrderCount;
            totalOrderCount += orderPrice.TradeBuyerSignedOrderCount;
            totalOrderCount += orderPrice.TradeNoCreatePayOrderCount;
            totalOrderCount += orderPrice.WaitBuyerConfirmGoodsOrderCount;
            totalOrderCount += orderPrice.WaitBuyerPayOrderCount;
            totalOrderCount += orderPrice.WaitSellerSendGoodsOrderCount;
            totalOrderCount += orderPrice.RefundingOrderCount;
            totalOrderCount += orderPrice.RefundOrderCount;
            totalOrderCount += orderPrice.TradeClosedOrderCount;
            totalOrderCount += orderPrice.TradeFinishedOrderCount;
            totalOrderCount += orderPrice.TradeClosedByTaoBaoOrderCount;

            return totalTradeCount == 0 ? 0 : totalOrderCount / totalTradeCount;
        }

        /// <summary>
        /// 获取已付款金额
        /// </summary>
        /// <param name="orderPrice"></param>
        /// <returns></returns>
        public int GetPaidCustomerCount(OrderPrice orderPrice)
        {
            int paidTradeCount = 0;
            int paidOrderCount = 0;

            paidTradeCount += orderPrice.WaitSellerSendGoodsTradeCount;
            paidTradeCount += orderPrice.SellerConsignedPartTradeCount;
            paidTradeCount += orderPrice.WaitBuyerConfirmGoodsTradeCount;
            paidTradeCount += orderPrice.TradeBuyerSignedTradeCount;
            paidTradeCount += orderPrice.TradeFinishedTradeCount;

            paidOrderCount += orderPrice.WaitSellerSendGoodsOrderCount;
            paidOrderCount += orderPrice.SellerConsignedPartOrderCount;
            paidOrderCount += orderPrice.WaitBuyerConfirmGoodsOrderCount;
            paidOrderCount += orderPrice.TradeBuyerSignedOrderCount;
            paidOrderCount += orderPrice.TradeFinishedOrderCount;

            return paidOrderCount == 0 ? 0 : paidTradeCount / paidOrderCount;
        }
    }
}
